﻿<%@ WebHandler Language="C#" Class="LoginHandler" %>

using System;
using System.Web;

public class LoginHandler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        string strResponseDoc = "";
        //用户信息业务处理类
        Chenxin.Lib.Passport.BLL.Hr_UserInfo hrUserInfo = new Chenxin.Lib.Passport.BLL.Hr_UserInfo();
        //用户信息类
        Chenxin.Lib.Passport.Entity.Hr_UserInfo entUserInfo;
        //登录用户工号
        string strUserCode = context.Request["usercode"];
        
        //登录用户密码
        string strPsw = context.Request["password"];

        //MD5加密后的密码
        string strMD5Psw;

        //根据工号取得用户信息
        entUserInfo = hrUserInfo.GetEntityByCode(strUserCode);
        //用户信息为空
        if (entUserInfo == null)
        {
            strResponseDoc = string.Format("({{result:'false',msg:'{0}'}})", "用户名或密码不正确！");
            context.Response.Write(strResponseDoc);
            return;
        }
        else
        {
            strMD5Psw = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strPsw, "MD5");
            if (entUserInfo.Validate == false)
            {
                strResponseDoc = string.Format("({{result:'false',msg:'{0}'}})", "该账号已经被冻结！");
                context.Response.Write(strResponseDoc);
                return;
            }
            if (entUserInfo.Password == strMD5Psw)
            {
                if (entUserInfo.NeedChgPass)
                {
                    strResponseDoc = string.Format("({{result:'needchgpwd',msg:'{0}',userid:'{1}'}})", "需要重置密码！", entUserInfo.UserId);
                }
                else
                {
                    //声称requestkey 并放入缓存，过期时间设置为20分钟
                    string strRequestKey = Guid.NewGuid().ToString().Replace("-", "");
                    string strCachedKey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_REQUEST, strRequestKey);
                    Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);

                    cs.Set(strCachedKey, entUserInfo.UserId, DateTime.Now.AddMinutes(20));

                    //用户登录
                    //用户Id保存到Cookie中
                    HttpCookie ckRequestKey = new HttpCookie("requestkey", strRequestKey);
                    context.Response.AppendCookie(ckRequestKey);

                    strResponseDoc = string.Format("({{result:'true',msg:'{0}'}})", "登录成功！");
                }
                
                context.Response.Write(strResponseDoc);
                RecordLoginLogs("登录成功", entUserInfo,"");
                return;
            }
            else
            {
                strResponseDoc = string.Format("({{result:'false',msg:'{0}'}})", "用户名或密码不正确！");
                context.Response.Write(strResponseDoc);
                RecordLoginLogs("登录失败", entUserInfo,"密码错误！");
                return;
            }
        }

        
    }
    
    /// <summary>
    /// 写登录日志
    /// </summary>
    /// <param name="status"></param>
    /// <param name="userCode"></param>
    /// <param name="userName"></param>
    /// <param name="departName"></param>
    public void RecordLoginLogs(string status, Chenxin.Lib.Passport.Entity.Hr_UserInfo hr, string description)
    {
        Chenxin.Lib.Hr.Entity.Hr_UserLoginLogs logsModel = new Chenxin.Lib.Hr.Entity.Hr_UserLoginLogs();
        logsModel.Login_Ip = GetClientIP();
        logsModel.Login_Time = DateTime.Now;
        logsModel.Login_UserCode = hr.UserCode;
        logsModel.Login_UserName = hr.UserName;
        logsModel.Login_CityId = hr.BelongCityId;
        logsModel.Login_City = hr.BelongCityName;
        logsModel.Login_Company = hr.BelongCompanyName;
        logsModel.Login_CompanyId = hr.BelongCompanyId;
        logsModel.Login_DepartId = hr.BelongDepartId;
        logsModel.Login_DepartName = hr.BelongDepart;
        logsModel.Login_Id = Guid.NewGuid();
        logsModel.Login_Status = status;
        logsModel.Login_Description = description;

        Chenxin.Lib.Hr.DAL.Hr_UserLoginLogs.Add(logsModel);
    }



    /// <summary>
    /// 获取IP地址
    /// </summary>
    /// <returns></returns>
    private string GetClientIP()
    {
        string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (null == result || result == String.Empty)
        {
            result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
        }

        if (null == result || result == String.Empty)
        {
            result = HttpContext.Current.Request.UserHostAddress;
        }
        return result;
    }
    public bool IsReusable {
        get {
            return false;
        }
    }
}